package com.jzo2o.foundations.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.enums.ServiceStatusEnum;
import com.jzo2o.foundations.mapper.RegionMapper;
import com.jzo2o.foundations.mapper.ServeItemMapper;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.properties.model.domain.Region;
import com.jzo2o.foundations.properties.model.domain.Serve;
import com.jzo2o.foundations.properties.model.domain.ServeItem;
import com.jzo2o.foundations.properties.model.domain.ServeSync;
import com.jzo2o.foundations.properties.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.properties.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.properties.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IServeSyncService;
import com.jzo2o.foundations.service.Iservice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * @author 11394
 * @version 1.0
 * @description: TODO
 * @date 2024/8/16 16:07
 */
@Service
@Transactional
public class IserviceImpl extends ServiceImpl<ServeMapper, Serve> implements Iservice {

    @Resource
    ServeMapper serveMapper;
    @Resource
    ServeItemMapper serveItemMapper;
    @Resource
    RegionMapper regionMapper;
    @Resource
    IServeSyncService iServeSyncService;

    @Override
    public PageResult<ServeResDTO> pageResult(ServePageQueryReqDTO servePageQueryReqDTO) {

        List<ServeResDTO> list = serveMapper.pageResult(servePageQueryReqDTO);

        PageResult<ServeResDTO> servePageQueryReqDTOPageResult = new PageResult<>(servePageQueryReqDTO.getPageNum(), servePageQueryReqDTO.getPageSize(), list);

        return servePageQueryReqDTOPageResult;
    }

    @Override
    public void addAll(List<ServeUpsertReqDTO> list) {

        for (ServeUpsertReqDTO serveUpsertReqDTO : list) {

            //判断服务是否启用中
            ServeItem serveItem = serveItemMapper.selectById(serveUpsertReqDTO.getServeItemId());
            if(serveItem.getActiveStatus()!= FoundationStatusEnum.ENABLE.getStatus()){
                throw  new ForbiddenOperationException("服务没有启用");
            }
            //判断一下这个服务是否已经添加过了
            QueryWrapper<Serve> serveQueryWrapper = new QueryWrapper<>();
            serveQueryWrapper.lambda().eq(Serve::getServeItemId,serveUpsertReqDTO.getServeItemId())
                    .eq(Serve::getRegionId,serveUpsertReqDTO.getRegionId());
            List<Serve> serves = baseMapper.selectList(serveQueryWrapper);
            if(serves.size()>0&&!serves.isEmpty()){
                throw  new ForbiddenOperationException("服务已经存在");
            }
            //转换为serve对象
            Serve serve = BeanUtil.toBean(serveUpsertReqDTO, Serve.class);
            //获取城市编码
            Region region = regionMapper.selectById(serveUpsertReqDTO.getRegionId());
            serve.setCityCode(region.getCityCode());
            int insert = baseMapper.insert(serve);

        }
    }

    @Override
    public void regionPriceUpdate(Long id, BigDecimal price) {

        //更新服务价格
        UpdateWrapper<Serve> serveUpdateWrapper = new UpdateWrapper<>();
        serveUpdateWrapper.lambda().set(Serve::getPrice,price).eq(Serve::getId,id);
        boolean update = this.update(serveUpdateWrapper);

        //同步服务表
        UpdateWrapper<ServeSync> serveSyncUpdateWrapper = new UpdateWrapper<>();
        serveSyncUpdateWrapper.lambda().set(ServeSync::getPrice,price).eq(ServeSync::getId,id);

        boolean update1 = iServeSyncService.update(serveSyncUpdateWrapper);
        if(!update){
            throw new ForbiddenOperationException("状态修改失败");
        }

    }

    @Override
    public void onSale(Long id) {

        //查询区域服务是否存在
        Serve serve = serveMapper.selectById(id);
        if(Objects.isNull(serve)){
            throw new ForbiddenOperationException("区域服务不从在");
        }

        //如果区域服务的状态是草稿或者是下架的时候才可以上架

        if(serve.getSaleStatus()!=FoundationStatusEnum.DISABLE.getStatus()&&serve.getSaleStatus()!=FoundationStatusEnum.INIT.getStatus()){

            throw new ForbiddenOperationException("状态必须是草稿或者下架");
        }

        Long serveItemId = serve.getServeItemId();

        ServeItem serveItem = serveItemMapper.selectById(serveItemId);

        if(serveItem.getActiveStatus()!=FoundationStatusEnum.ENABLE.getStatus()){
            throw new ForbiddenOperationException("服务没有启用");
        }

        //修改状态为上架
        boolean update = lambdaUpdate().set(Serve::getSaleStatus, FoundationStatusEnum.ENABLE.getStatus()).eq(Serve::getId, id).update();
        if(!update){
            throw new ForbiddenOperationException("状态修改失败");
        }
    }

    @Override
    public void offSale(Long id) {
        //查询区域服务是否存在
        Serve serve = serveMapper.selectById(id);
        if(Objects.isNull(serve)){
            throw new ForbiddenOperationException("区域服务不从在");
        }

        //如果区域服务是上架才可以下架

        if(serve.getSaleStatus()!=FoundationStatusEnum.ENABLE.getStatus()){

            throw new ForbiddenOperationException("状态必须是上架");
        }

        Long serveItemId = serve.getServeItemId();

        ServeItem serveItem = serveItemMapper.selectById(serveItemId);

        if(serveItem.getActiveStatus()!=FoundationStatusEnum.ENABLE.getStatus()){
            throw new ForbiddenOperationException("服务没有启用");
        }

        //修改状态为下架
        boolean update = lambdaUpdate().set(Serve::getSaleStatus, FoundationStatusEnum.DISABLE.getStatus()).eq(Serve::getId, id).update();
        if(!update){
            throw new ForbiddenOperationException("状态修改失败");
        }
    }

    @Override
    public void dletedByid(Long id) {

        //判断状态如果不是草稿状态禁止删除
        Serve serve = serveMapper.selectById(id);

        if(serve.getSaleStatus()!=FoundationStatusEnum.INIT.getStatus()){
            throw  new ForbiddenOperationException("不是草稿状态下禁止删除");
        }

        int i = serveMapper.deleteById(id);
    }

    @Override
    public void onHot(long id) {

        //修改热门的状态
        boolean update = lambdaUpdate().set(Serve::getIsHot, ServiceStatusEnum.DISABLE.getStatus()).eq(Serve::getId, id).update();

        if(!update){
            throw new ForbiddenOperationException("热门设置失败");
        }
    }

    @Override
    public void offHot(long id) {
        //修改热门的状态
        boolean update = lambdaUpdate().set(Serve::getIsHot, ServiceStatusEnum.INIT.getStatus()).eq(Serve::getId, id).update();

        if(!update){
            throw new ForbiddenOperationException("取消热门失败");
        }
    }
}


